<?php

/**
 * This is the model class for table "mms_document".
 *
 * The followings are the available columns in table 'mms_document':
 * @property integer $documentID
 * @property string $docName
 * @property string $fileName
 * @property string $documentDate
 * @property integer $typeID
 * @property integer $projectID
 * @property string $active
 * @property string $uploadUser
 * @property string $uploadTime
 *
 * The followings are the available model relations:
 * @property Project $project
 * @property DocumentType $type
 */
class Document extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mms_document';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('projectID', 'required'),
			array('documentID, typeID, projectID', 'numerical', 'integerOnly'=>true),
			array('docName', 'length', 'max'=>225),
			array('fileName', 'length', 'max'=>255),
			array('active', 'length', 'max'=>4),
			array('uploadUser', 'length', 'max'=>20),
			array('documentDate, uploadTime', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('documentID, docName, fileName, documentDate, typeID, projectID, active, uploadUser, uploadTime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'project' => array(self::BELONGS_TO, 'Project', 'projectID'),
			'type' => array(self::BELONGS_TO, 'DocumentType', 'typeID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'documentID' => 'รหัสเอกสาร',
			'docName' => 'ชื่อเอกสาร',
			'fileName' => 'ชื่อไฟล์',
			'documentDate' => 'วันที่เอกสาร',
			'typeID' => 'รหัสชนิดเอกสาร',
			'projectID' => 'รหัสโครงการสารนิพนธ์',
			'active' => 'สถานะการเปิดใช้งาน',
			'uploadUser' => 'ผู้อัพโหลดเอกสาร',
			'uploadTime' => 'วันที่อัพโหลดเอกสาร',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('documentID',$this->documentID);
		$criteria->compare('docName',$this->docName,true);
		$criteria->compare('fileName',$this->fileName,true);
		$criteria->compare('documentDate',$this->documentDate,true);
		$criteria->compare('typeID',$this->typeID);
		$criteria->compare('projectID',$this->projectID);
		$criteria->compare('active',$this->active,true);
		$criteria->compare('uploadUser',$this->uploadUser,true);
		$criteria->compare('uploadTime',$this->uploadTime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Document the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
